<template>
	<view class="">
		<view class="mainpadding xiaohong">本单据共计{{dataAll.x_total_price}}元，未付款{{dataAll.arrears_price}}元</view>
		<view class="mainpadding flexcenter">
			<input type="number" placeholder="请输入支付金额"
				style="font-size: 80rpx; height: 80rpx; margin: 130rpx 0; font-weight: bold;text-align: center;color: #ff0000;"
				v-model="pay_price">
		</view>

		<view class="mainpadding_top xiaohui">本次收未付款<text style="color: #28C445;">{{pay_price || 0}}</text>元，剩余未付款<text
				class="xiaohong">{{Number(dataAll.arrears_price)-Number(pay_price)>0?Number(dataAll.arrears_price)-Number(pay_price):0}}</text>元
		</view>
		<view class="mainpadding_top xiaohui">我的余额：<text style="color: #28C445;">{{mymoney || 0}}</text>元
		</view>
		<view class="mainpadding">
			<view class="mainpadding2 ffffff radius">
				<u-radio-group v-model="pay_type" iconPlacement="right" placement="column">
					<u-radio activeColor="#f5ac4f" label="余额支付" name="40"></u-radio>
					<u-radio activeColor="#f5ac4f" label="微信支付" name="10"></u-radio>
				</u-radio-group>
			</view>
		</view>
		<view class="posibottom mainpadding">
			<view class="submitbtn" @click="payOk">支付</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/commonFun/httpRequest.js");
	export default {
		data() {
			return {
				pay_type: "10",
				dataAll: {},
				pay_price: "",
				mymoney: 0,
			}
		},
		onLoad(options) {
			this.dataAll = JSON.parse(decodeURIComponent(options.data));
			this.pay_price = Number(this.dataAll.arrears_price)
			this.userMy()
		},
		methods: {
			userMy() {
				let _this = this
				httpRequest.request('/customapi/user/index', 'POST', {}).then(res => {
					if (res.code == 1) {
						this.mymoney = res.data.money
					} else {
						httpRequest.toast(res.msg);
					}
				})
			},
			payOk() {
				let _this = this;
				if (Number(this.pay_price) == 0) {
					httpRequest.toast("请输入支付金额！")
					return false
				}
				if (Number(this.pay_price) > Number(this.dataAll.arrears_price)) {
					httpRequest.toast("支付金额不能大于未付款金额！")
					return false
				}
				httpRequest.request('/customapi/Saleorder/addSaleOrderPay', 'GET', {
					sale_order_id: this.dataAll.id,
					pay_type: this.pay_type,
					pay_price: this.pay_price,
				}).then(res => {
					if (res.code == 1) {
						if (this.pay_type == 10) {
							let wxpaymsg = JSON.parse(res.data.expend.pay_info)
							this.wxPay(wxpaymsg)
						} else {
							httpRequest.toast(res.msg)
							uni.redirectTo({
								url: "/pages/shoppingCart/payOk"
							})
						}
					} else {
						httpRequest.toast(res.msg)
					}

				})

			},
			// 微信支付
			wxPay(wxpay) {
				let _this = this;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: wxpay.timeStamp,
					nonceStr: wxpay.nonceStr,
					package: wxpay.package,
					signType: wxpay.signType,
					paySign: wxpay.paySign,
					success: function(res) {
						// 支付成功
						uni.redirectTo({
							url: "/pages/shoppingCart/payOk"
						})
					},
					fail: function(err) {
						// 支付失败
						_this.paycatchresult = true
					}
				});
			},
		}
	}
</script>

<style>
	/deep/.u-radio-group--row {
		display: block;
	}

	/deep/.u-radio {
		margin-top: 30rpx;
	}

	.btn {
		margin-top: 30rpx;
		text-align: center;
		padding: 29rpx 0;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #FFFFFF;
		border-radius: 50rpx;
		background-color: #F5AC4F;
	}
</style>